/*
 * Copyright (c) 2019, HePei
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-1-15     HpDadi		v0.1          
 */

#include <at.h>
#include <string.h>
#include <stdio.h>


#define at_JDY_10M_DeviceName "uart3"
#define at_JDY_10M_RECV_BUFF_LEN 512

struct at_JDY_10M_STR{
	char NETID[13];  //actully is hex char should below F
	char name[19];   //any char
	char pass[13];   //any char
	char maddr[3];   //actully is hex char should below F
};
typedef struct at_JDY_10M_STR * at_JDY_10M_STRP;

static struct at_JDY_10M_STR at_JDY_10M_str ={
	"860562100100","HPFAMILY","ADFJADJ;FDFA","00"
};
static at_JDY_10M_STRP at_JDY_10M_strp = &at_JDY_10M_str;

/**
 * set AT socket event notice callback
 *
 * @param event notice event
 * @param cb notice callback
 */


static void urc_recv_VER(const char *data, rt_size_t size)
{
	//todo: show or save the VER;
	rt_kprintf("The version of JDY-10 is: %s",data);
	
}

 
static struct at_urc urc_table[] = {
	{"JDY","\r\n", urc_recv_VER},
};
 


static int set_NETID(void){
	at_response_t resp;
	resp = at_create_resp(64, 1, rt_tick_from_millisecond(500));
	if(resp == RT_NULL){
		return RT_ENOMEM;
	}
	at_obj_exec_cmd(at_client_get(at_JDY_10M_DeviceName),resp,"AT+NETID%s",at_JDY_10M_strp->NETID);
	if(memcmp("+OK",resp->buf,3) == 0){
		at_delete_resp(resp);
		return RT_EOK;
	}else{
		at_delete_resp(resp);
		return RT_ERROR;
	}
}

static int read_NETID(void){
	at_response_t resp;
	resp = at_create_resp(64, 1, rt_tick_from_millisecond(500));
	if(resp == RT_NULL){
		return RT_ENOMEM;
	}
	at_obj_exec_cmd(at_client_get(at_JDY_10M_DeviceName),resp,"AT+NETID");
	at_resp_parse_line_args(resp,1,"+NETID=%s",at_JDY_10M_strp->NETID);
	at_delete_resp(resp);
	//todo:if success;
	return RT_EOK;
}

static int set_NAME(void){
	at_response_t resp;
	resp = at_create_resp(64, 1, rt_tick_from_millisecond(500));
	if(resp == RT_NULL){
		return RT_ENOMEM;
	}
	at_obj_exec_cmd(at_client_get(at_JDY_10M_DeviceName),resp,"AT+NAME%s",at_JDY_10M_strp->name);
	if(memcmp("+OK",resp->buf,3) == 0){
		at_delete_resp(resp);
		return RT_EOK;
	}else{
		at_delete_resp(resp);
		return RT_ERROR;
	}
}

static int read_NAME(void){
	at_response_t resp;
	resp = at_create_resp(64, 1, rt_tick_from_millisecond(500));
	if(resp == RT_NULL){
		return RT_ENOMEM;
	}
	at_obj_exec_cmd(at_client_get(at_JDY_10M_DeviceName),resp,"AT+NAME");
	at_resp_parse_line_args(resp,1,"+NAME=%s",at_JDY_10M_strp->name);
	at_delete_resp(resp);
	//todo:if success;
	return RT_EOK;
}


static int set_MADDR(void){
	at_response_t resp;
	resp = at_create_resp(64, 1, rt_tick_from_millisecond(500));
	if(resp == RT_NULL){
		return RT_ENOMEM;
	}
	at_obj_exec_cmd(at_client_get(at_JDY_10M_DeviceName),resp,"AT+MADDR%s",at_JDY_10M_strp->maddr);
	if(memcmp("+OK",resp->buf,3) == 0){
		at_delete_resp(resp);
		return RT_EOK;
	}else{
		at_delete_resp(resp);
		return RT_ERROR;
	}
}

static int read_MADDR(void){
	at_response_t resp;
	resp = at_create_resp(64, 1, rt_tick_from_millisecond(500));
	if(resp == RT_NULL){
		return RT_ENOMEM;
	}
	at_obj_exec_cmd(at_client_get(at_JDY_10M_DeviceName),resp,"AT+MADDR");
	at_resp_parse_line_args(resp,1,"+MADDR=%s",at_JDY_10M_strp->maddr);
	at_delete_resp(resp);
	//todo:if success
	return RT_EOK;
}


static int send_data(uint8_t maddr,char* data)
{
	uint16_t length = 0;
	length = strlen(data);
	while(length){
		if(length <= 10){
		//todo: send
			length = 0;
		}else if(length >10){
		//todo:send 10
			length-=10;
			data+=10;
		}
	}
}


int at_JDY_10M_init(void)
{
	/*Initialize at client*/
	at_client_init(at_JDY_10M_DeviceName,at_JDY_10M_RECV_BUFF_LEN);
	/* register URC data execution function  */
	at_obj_set_urc_table(at_client_get(at_JDY_10M_DeviceName),urc_table,sizeof(urc_table) / sizeof(urc_table[0]));

	read_NETID();
	read_NAME();
	read_MADDR();
	//todo:check RTOK
	return RT_EOK;
}

static void at_JDY_10M_set(int argc, char **argv)
{
	if(argc == 1)
	{
		set_NETID();
		set_NAME();
	}else if(argc ==3)
	{
		if(memcmp("MADDR",argv[1],5) == 0){
			char temp[3];
			sscanf(argv[2],"%s",temp);
			
			if(strlen(temp) != 2){
				rt_kprintf("the MADDR should be 2 char");
			}else{
				memcpy(at_JDY_10M_strp->maddr,temp,2);
			}
			
			set_MADDR();
		}else if(memcmp("NETID",argv[1],5) == 0){
			char temp[13];
			sscanf(argv[2],"%s",temp);
			
			if(strlen(temp) != 12){
				rt_kprintf("the NETID should be 12 char");
			}else{
				memcpy(at_JDY_10M_strp->NETID,temp,12);
			}
			
			set_NETID();
		}else if(memcmp("NAME",argv[1],4)==0){		
			sscanf(argv[2],"%s",at_JDY_10M_strp->name);
			set_NAME();
		}
	}
}

MSH_CMD_EXPORT(at_JDY_10M_set, at_JDY_10M NAME/NETID/MADDR value(_char));

